package com.woniu.microworkorderserver.feign;

import com.alibaba.fastjson.JSONObject;
import com.github.pagehelper.PageInfo;
import com.woniu.microcommonsserver.entity.UserInfoEntity;
import com.woniu.microcommonsserver.param.PageResult;
import com.woniu.microcommonsserver.param.ResponseMsg;
import com.woniu.microworkorderserver.param.UserResult;
import com.woniu.microworkorderserver.workgroupmag.mapper.WorkGroupUserMapper;
import com.woniu.microworkorderserver.workgroupmag.service.WorkGroupUserService;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.*;
import java.util.concurrent.TimeUnit;

@RestController
@RequestMapping("/feign")
public class FeignController {

    @Resource
    private UserServerFeign userServerFeign;
    @Resource
    private WorkGroupUserService workGroupUserServiceImpl;
    @Resource
    private AssetServerFeign assetServerFeign;
    @Resource
    private ValueOperations valueOperations;

    @RequestMapping(value = "/getUser",produces = MediaType.APPLICATION_JSON_VALUE)
    public ResponseMsg getData(){
        return userServerFeign.findFour();
    }

    @RequestMapping(value = "/findByIdUser",produces = MediaType.APPLICATION_JSON_VALUE)
    public ResponseMsg findByIdUser(@RequestParam("workGroupId")long workGroupId){
        List<UserInfoEntity> users = (List<UserInfoEntity>) userServerFeign.findFour().getData();
        int[] ids = workGroupUserServiceImpl.findByGroupId(workGroupId);
        Map<String,Object> map = new HashMap<>();
        map.put("users",users);
        map.put("ids",ids);
        return ResponseMsg.builder().code(200).msg("查询成功").data(map).build();
    };

    @RequestMapping(value = "/findByIdUser2",produces = MediaType.APPLICATION_JSON_VALUE)
    public ResponseMsg findByIdUser2(@RequestParam("workGroupId") long id){
        System.out.println(id);
        int[] ids = workGroupUserServiceImpl.findByGroupId(id);
        return userServerFeign.searchbyids(ids);
    }

    @RequestMapping(value = "/findPage",produces = MediaType.APPLICATION_JSON_VALUE)
    public ResponseMsg findPage(@RequestBody Map<String,Object> map){

//        PageInfo pages = new PageInfo();
//        PageResult<UserResult> results = new PageResult<>();
//        if (valueOperations.get("pages") != null){
//            Object obj1 =  valueOperations.get("pages");
//            String jsonString = JSONObject.toJSONString(obj1);
//            pages = JSONObject.parseObject(jsonString, PageInfo.class);
//        }else{
//            Object obj = userServerFeign.search(map).getData();
//            String jsonString = JSONObject.toJSONString(obj);
//            pages = JSONObject.parseObject(jsonString, PageInfo.class);
//
//            //将数据存放在redis中
//            if (pages != null) {
//                valueOperations.set("pages", pages, 8, TimeUnit.HOURS);
//            }
//        }
        Object obj = userServerFeign.search(map).getData();
        String jsonString = JSONObject.toJSONString(obj);
        PageInfo page = JSONObject.parseObject(jsonString,PageInfo.class);

        //封装结果集
        List<UserResult> userResults = workGroupUserServiceImpl.findByUserId(page);

        PageResult<UserResult> results = new PageResult<>();
        if(page != null){
            results.setTotal(page.getTotal());
        }
        results.setRows(userResults);
        return ResponseMsg.builder().code(200).msg("查询成功").data(results).build();
    }

}
